<?php
   
defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Action');

class Controller_Users extends Common_Action {

	function __construct($ctlName)   {
		parent::__construct($ctlName);
		$this->tplDir = TPL_F;
		//$data = array(
		//	'header' => TPL . $this->tplDir . 'header.php',
		//	'footer' => TPL . $this->tplDir . 'footer.php'
		//);
		//$this->tpl->assign($data);
	}
	
	function index() {
		//redirect(url());
		dump($_SESSION);
	}

	function login() {
		$_modelUser = FLEA::getSingleton('Model_Users');
		
		if ( $this->_isPost() ) {
			$result = $_modelUser->login($_POST);
			if ( !empty($result) )
				js_alert($result, null, url('users', 'login'));
			else {
				//dump($_SESSION);
				redirect(url());
			}
		} else {
			$viewData['base'] = $_modelUser->getForm('login');
			$this->_out('users/login', $viewData);
		}
	}
	
	function logout() {
		$rbac = FLEA::getSingleton('FLEA_RBAC');
		$rbac->clearUser();
		redirect(url());
	}
	
	function register() {
		$_modelUser = FLEA::getSingleton('Model_Users');
		
//		$user = array(
//			'username' => 'zero',
//			'password' => 'gkim',
//			'email' => 'test@test.test',
//			'stuNum' => '2007051377',
//			'trueName' => '赖永华�',
//			'userType' => '1',
//			'sex' => '1',
//			'poor' => '2',
//			'bankCardNum' => '2007051377',
//			'politics' => '1',
//			'tel' => '2007051377',
//			'room' => '2007051377',
//			'college' => '信息科学技术学院�',
//			'department' => '电子工程系',
//			'major' => 电子信息工程',
//			'province' => '广东省�',
//		);
//		$_modelUser->register($user);

		if ( $this->_isPOST() ) {
			$result = $_modelUser->register($_POST);
			
			if ( count($result) == 0 )
				js_alert('注册成功', null, url());
			else
				js_alert($result[0], null, url());
		} else {
			$viewData = array(
				'form' => $_modelUser->getForm('register'),
				'majors' => FLEA::loadFile('Configs_Majors')
			);
			$this->_out('users/register', $viewData);
		}
	}
	
	function modify() {
		$_modelUser = FLEA::getSingleton('Model_Users');
		if ( $this->_isPOST() ) {
			$result = $_modelUser->update($_POST);
			if (!empty($result)) {
				foreach ($result as $re) {
					$err .= $re . ',';
				}
				js_alert($err, 'history.back();');
			}
			js_alert('修改成功！', null, $this->_url('modify'));
		} else {
			$userData = $_modelUser->getInfo($this->user['userid']);
			$viewData = array(
				'form' => $_modelUser->getForm('modify', $userData),
				'user' => $userData,
				'majors' => FLEA::loadFile('Configs_Majors')
			);
			$this->_out('user/modify', $viewData);
		}
	}
	
	function test() {
		$_tableUser = FLEA::getSingleton('Table_Users');
		$_tableUser->enableLink('work');
		$user = $_tableUser->findByUsername('zero');
		//$_modelUser = FLEA::getSingleton('Model_Users');
		//$user1 = $_modelUser->getUserById(1);
		dump($user['roles'][0]['role_id']);
	}

}